"""
Blog blueprint views for the Flask Blueprints demo.
"""
from flask import render_template, request, redirect, url_for, flash
from . import blog


# Sample data for demonstration
posts_data = [
    {
        'id': 1,
        'title': 'Introduction to Flask Blueprints',
        'excerpt': 'Learn how to organize your Flask application using blueprints...',
        'content': '<p>Flask blueprints are a powerful feature that allows you to organize your application into reusable components.</p><p>In this post, we\'ll explore how to create and use blueprints effectively.</p>',
        'author': 'Alice Developer',
        'date': '2023-01-15',
        'category': 'Technology',
        'reading_time': 5
    },
    {
        'id': 2,
        'title': 'Building RESTful APIs with Flask',
        'excerpt': 'A guide to creating RESTful web services using Flask...',
        'content': '<p>RESTful APIs are an essential part of modern web development.</p><p>This post will show you how to build robust APIs with Flask.</p>',
        'author': 'Bob Coder',
        'date': '2023-01-20',
        'category': 'Technology',
        'reading_time': 8
    },
    {
        'id': 3,
        'title': 'Advanced Flask Patterns',
        'excerpt': 'Explore advanced design patterns for Flask applications...',
        'content': '<p>Once you\'ve mastered the basics of Flask, it\'s time to learn some advanced patterns.</p><p>We\'ll cover application factories, blueprints, and more.</p>',
        'author': 'Charlie Engineer',
        'date': '2023-02-01',
        'category': 'Education',
        'reading_time': 10
    }
]

categories_data = [
    {'id': 1, 'name': 'Technology', 'description': 'Posts about technology and programming', 'post_count': 12},
    {'id': 2, 'name': 'Lifestyle', 'description': 'Posts about lifestyle and personal development', 'post_count': 8},
    {'id': 3, 'name': 'Travel', 'description': 'Posts about travel experiences and tips', 'post_count': 5},
    {'id': 4, 'name': 'Food', 'description': 'Posts about cooking and food reviews', 'post_count': 7},
    {'id': 5, 'name': 'Education', 'description': 'Posts about learning and education', 'post_count': 9}
]

comments_data = [
    {'id': 1, 'post_id': 1, 'author': 'Dave Reader', 'content': 'Great introduction to blueprints!', 'date': '2023-01-16'},
    {'id': 2, 'post_id': 1, 'author': 'Eve Learner', 'content': 'This helped me organize my app much better.', 'date': '2023-01-17'},
    {'id': 3, 'post_id': 2, 'author': 'Frank Developer', 'content': 'Looking forward to more API content.', 'date': '2023-01-21'}
]


@blog.route('/')
def index():
    """Blog index page showing all posts."""
    return render_template('blog/index.html', posts=posts_data)


@blog.route('/post/<int:post_id>')
def post(post_id):
    """Individual blog post page."""
    # Find the post with the given ID
    post = next((p for p in posts_data if p['id'] == post_id), None)
    
    if post is None:
        flash('Post not found.', 'error')
        return redirect(url_for('blog.index'))
    
    # Get comments for this post
    comments = [c for c in comments_data if c['post_id'] == post_id]
    
    return render_template('blog/post.html', post=post, comments=comments)


@blog.route('/create', methods=['GET', 'POST'])
def create():
    """Create a new blog post."""
    if request.method == 'POST':
        title = request.form.get('title')
        category = request.form.get('category')
        content = request.form.get('content')
        
        if title and category and content:
            # In a real app, you would save this to a database
            flash(f'Post "{title}" created successfully!', 'success')
            return redirect(url_for('blog.index'))
        else:
            flash('Please fill in all fields.', 'error')
    
    return render_template('blog/create.html')


@blog.route('/categories')
def categories():
    """Blog categories page."""
    return render_template('blog/categories.html', categories=categories_data)


@blog.route('/category/<int:category_id>')
def category(category_id):
    """Posts in a specific category."""
    # Find the category with the given ID
    category = next((c for c in categories_data if c['id'] == category_id), None)
    
    if category is None:
        flash('Category not found.', 'error')
        return redirect(url_for('blog.categories'))
    
    # Filter posts by category (in a real app, this would be a database query)
    category_posts = [p for p in posts_data if p['category'] == category['name']]
    
    return render_template('blog/index.html', posts=category_posts)


@blog.route('/post/<int:post_id>/comment', methods=['POST'])
def add_comment(post_id):
    """Add a comment to a post."""
    author = request.form.get('author')
    content = request.form.get('content')
    
    if author and content:
        # In a real app, you would save this to a database
        flash('Comment added successfully!', 'success')
    else:
        flash('Please fill in all fields.', 'error')
    
    return redirect(url_for('blog.post', post_id=post_id))